﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BikeSystem.Models;
using BikeSystem.BLL;

namespace BikeUI
{
    public partial class FormChangePassword : Form
    {
        bool hasError = false;

        protected Account currentAccount = null;

        public FormChangePassword()
        {
            InitializeComponent();
        }

        public virtual void CurrentAccount(Account account)
        {
            currentAccount = account;
            lblLoginId.Text = account.LoginId;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            hasError = false;
            errorProvider1.Clear();

            txtAlphaNumericString_Validated(txtPassword);
            txtAlphaNumericString_Validated(txtNewPassword);
            txtAlphaNumericString_Validated(txtReEntry);

            if (!txtNewPassword.Text.Equals(txtReEntry.Text))
            {
                this.errorProvider1.SetError(txtReEntry, "This new password not match.");
                hasError = true;
            }

            if (!txtPassword.Text.Equals(currentAccount.LoginPwd))
            {
                this.errorProvider1.SetError(txtPassword, "This password not correct.");
                hasError = true;
            }

            if(!hasError){

                AccountManager accountManager = new AccountManager();
                currentAccount.LoginPwd = txtNewPassword.Text;
                CommonMessage message = accountManager.UpdateAccount(currentAccount);

                MessageBox.Show(message.Message,message.Caption);
                if(message.Success){
                    this.Close();
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        /// <summary>
        /// Validate that the text box contains only numbers
        /// or letters
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtAlphaNumericString_Validated(TextBox txtObject)
        {
            bool bTest = txtAlphaNumericStringIsValid(txtObject);
            if (bTest == true)
            {
                this.errorProvider1.SetError(txtObject, "This field must contain only alphanumerics");
                hasError = true;
            }
            else
            {
                this.errorProvider1.SetError(txtObject, "");
            }
        }

        /// <summary>
        /// Test for non-alpha values in the text box and
        /// return the results
        /// </summary>
        /// <returns>boolean</returns>
        private bool txtAlphaNumericStringIsValid(TextBox txtObject)
        {
            // Make sure the string is not empty first
            if (txtObject.Text == string.Empty)
            {
                return true;
            }
            // check for alphanumeric values
            char[] testArr = txtObject.Text.ToCharArray();
            bool testBool = false;
            for (int i = 0; i < testArr.Length; i++)
            {
                if (!char.IsLetter(testArr[i]) && !char.IsNumber(testArr[i]))
                {
                    testBool = true;
                }
            }
            return testBool;
        }
    }
}
